﻿
//自定义表单渲染
(function ($, ibpm) {
    "use strict";

    function getFontHtml(verify) {
        var res = "";
        switch (verify) {
            case "NotNull":
            case "Num":
            case "Email":
            case "EnglishStr":
            case "Phone":
            case "Fax":
            case "Mobile":
            case "MobileOrPhone":
            case "Uri":
                res = '<font face="宋体">*</font>';
                break;
        }
        return res;
    }

    function getTdValidatorHtml(verify) {
        var res = "";
        if (verify != "") {
            res = 'isvalid="yes" checkexpession="' + verify + '"';
        }
        return res;
    }
    // 绘制表单
    $.fn.ibpm_CustmerFormRender = function (data, UploaderId, isOnlyRead) {
        var $this = $(this);
        var compontsMap = {};

        var iLen = data.length;
        var $ul;
        var $container;

        if (iLen > 1) {
            var html = '<div class="M-form-tabs" id="M_form_tabs">';
            html += '<ul class="nav nav-tabs"></ul></div>';
            html += '<div class="tab-content M-tab-content" id="M_tab_content">';
            html += '</div>';

            $this.append(html);
            $('#M_form_tabs').ibpm_FormTab();
            $ul = $('#M_form_tabs ul');
            $container = $('#M_tab_content');
        }
        else {
            $container = $this;
        }

        $this[0].compontsMap = compontsMap;

        for (var i = 0; i < iLen; i++) {
            var $content = $('<div class="M-form-wrap row"></div>');
            $container.append($content);
            for (var j = 0, jLen = data[i].componts.length; j < jLen; j++) {
                var compont = data[i].componts[j];
                if (!!compont.field) {
                    compontsMap[compont.field] = compont.id;
                }
                if (!!compont.ExId) {
                    compont.ExId = UploaderId;
                }
                if (isOnlyRead) {
                    if (!!compont.OnlyRead) {
                        compont.OnlyRead = "1";
                    }
                }
                var $row = $('<div class="col-sm-' + (12 / parseInt(compont.proportion)) + ' M-form-item" ></div>');
                var $title = $(' <div class="M-form-item-title">' + compont.title + getFontHtml(compont.verify) + '</div>');
                if (compont.title != '') {
                    $row.append($title);
                }
                $content.append($row);
                var $compont = $.BPM_FormComponents[compont.type].renderTable(compont, $row);
                if (!!$compont && !!compont.verify && compont.verify != "") {
                    $compont.attr('isvalid', 'yes').attr('checkexpession', compont.verify);
                }
            }

            // 如果大于一个选项卡，需要添加选项卡，否则不需要
            if (iLen > 1) {
                $ul.append('<li><a data-value="' + data[i].id + '">' + data[i].text + '</a></li>');
                $content.addClass('tab-pane').attr('id', data[i].id);
                if (i == 0) {
                    $ul.find('li').trigger('click');
                }
            }
        }

        $('.M-form-wrap').ibpm_scroll();

        return compontsMap;
    };

    // 验证自定义表单数据
    $.BPM_ValidCustmerform = function () {
        var validateflag = true;
        var validHelper = ibpm.validator;
        $('body').find("[isvalid=yes]").each(function () {
            var $this = $(this);
            if ($this.parent().find('.M-field-error-info').length > 0) {
                //validateflag = false;
                //return true;  //有可能是动态赋值，就再验证一次
            }
            var checkexpession = $(this).attr("checkexpession");
            var checkfn = validHelper['is' + checkexpession];
            if (!checkexpession || !checkfn) {
                return false;
            }
            var errormsg = $(this).attr("errormsg") || "";
            var value;
            var type = $this.attr('type');
            if (type == 'Mselect') {
                value = $this.ibpm_selectGet();
            }
            else if (type == 'GirdSelect') {
                value = $this.ibpm_GirdSelectGet();
            }
            else if (type == 'upload') {
                value = $this.ibpm_UploaderGet();
            }
            else if (type == 'pic') {
                value = $this.ibpm_picGet();
            }
            else {
                if ($this.hasClass('edui-default')) {
                    if ($this[0].ue) {
                        value = $this[0].ue.getContent();
                    }
                }
                else {
                    value = $this.val();
                }
            }
            var r = { code: true, msg: '' };
            if (checkexpession == 'LenNum' || checkexpession == 'LenNumOrNull' || checkexpession == 'LenStr' || checkexpession == 'LenStrOrNull') {
                var len = $this.attr("length");
                r = checkfn(value, len);
            } else {
                r = checkfn(value);
            }
            if (!r.code) {
                validateflag = false;
                $.BPM_ValidformMessage($this, errormsg + r.msg);
            }
        });
        return validateflag;
    }

    // 获取自定义表单数据
    $.fn.ibpm_GetCustmerformData = function (UploaderId) {
        var resdata = {};
        $(this).find('input,select,textarea,.M-select,.M-formselect,.M-Uploader-wrap,.M-Pic-wrap,.edui-default').each(function (r) {
            var $self = $(this);
            var id = $self.attr('id') || $self.attr('name');
            if (!!id) {
                var type = $self.attr('type');
                switch (type) {
                    case "checkbox":
                        if ($self.is(":checked")) {
                            if (resdata[id] != undefined && resdata[id] != '') {
                                resdata[id] += ',';
                            }
                            else {
                                resdata[id] = '';
                            }
                            resdata[id] += $self.val();
                        }
                        break;
                    case "radio":
                        if ($self.is(":checked")) {
                            resdata[id] = $self.val();
                        }
                        break;
                    case "Mselect":
                        resdata[id] = $self.ibpm_selectGet();
                        break;
                    case "GirdSelect":
                        resdata[id] = $self.ibpm_GirdSelectGet();
                        break;
                    case "upload":
                        if (UploaderId != null && UploaderId != undefined && UploaderId != '')
                            resdata[id] = UploaderId;
                        else
                            resdata[id] = $self.ibpm_UploaderGet();
                        break;
                    case "pic":
                        if (UploaderId != null && UploaderId != undefined && UploaderId != '')
                            resdata[id] = UploaderId;
                        else
                            resdata[id] = $self.ibpm_picGet();
                        break;
                    default:
                        if ($self.hasClass('currentInfo')) {
                            resdata[id] = $self[0].Mvalue;
                        }
                        else if ($self.hasClass('edui-default')) {
                            if ($self[0].ue) {
                                resdata[id] = $self[0].ue.getContent();
                            }
                        }
                        else {
                            var value = $self.val();
                            resdata[id] = $.trim(value);
                        }
                        break;
                }
            }
        });
        return resdata;
    }
    // 设置自定义表单数据
    $.fn.ibpm_SetCustmerformData = function (data) {// 设置表单数据
        var compontsMap = $(this)[0].compontsMap;
        for (var field in data) {
            var value = data[field];
            var id = compontsMap[field];
            var $obj = $('#' + id);
            if (!$obj.length || $obj.length == 0) {
                var vs = (value + "").split(',');
                for (var i = 0, l = vs.length; i < l; i++) {
                    _setvalue(vs[i]);
                }
                function _setvalue(_value) {
                    var _$obj = $('input[name="' + id + '"][value="' + _value + '"]');
                    if (!!_$obj.length && _$obj.length > 0) {
                        if (!_$obj.is(":checked")) {
                            _$obj.trigger('click');
                        }
                    }
                    else {
                        setTimeout(function () {
                            _setvalue(_value);
                        }, 100);
                    }
                }
            }
            else {
                var type = $obj.attr('type');
                if ($obj.hasClass("M-input-wdatepicker")) {
                    type = "datepicker";
                }
                switch (type) {
                    case "Mselect":
                        $obj.ibpm_selectSet(value);
                        break;
                    case "GirdSelect":
                        $obj.ibpm_GirdSelectSet(value);
                        break;
                    case "datepicker":
                        $obj.val(ibpm.formatDate(value, 'yyyy-MM-dd'));
                        break;
                    case "upload":
                        $obj.ibpm_UploaderSet(value);
                        break;
                    case "pic":
                        $obj.ibpm_picSet(value);
                        break;
                    default:
                        if ($obj.hasClass('currentInfo')) {
                            $obj[0].Mvalue = value;
                            $obj.val(value);
                        }
                        else if ($obj.hasClass('edui-default')) {
                            ueSet($obj[0].ue, value);
                            //$obj[0].ue.setContent(value);
                        }
                        else {
                            $obj.val(value);
                        }
                        break;
                }
            }
        }
    };

    function ueSet(ue, content) {
        ue.ready(function () {
            ue.setContent(content);
        });
    }

})(jQuery, top.ibpm);